<script setup lang="ts">
import { get, loadFile, message, post } from '@/utils'
import useModal from '@/hooks/useModal'
import ViewRecords from './components/ViewRecords.vue'
import TimeModal from './components/TimeModal.vue'
import { BtnPermission } from '@/enum/permission'
import useTable from '@/hooks/useTable'
const { store } = useModal()
const { store: viewStore } = useModal()
const { formatColumns } = useTable()
const searchQuery = reactive({})
const formRef = ref()
const actionRef = ref()

const changeStatus = item => {
  const { jobId, status } = item
  const url = status == 0 ? '/server-system/admin/schedule/pause' : '/server-system/admin/schedule/resume'
  post({ url: url + '?ids=' + jobId }, ({ code, msg }) => {
    if (code == 200) actionRef.value.reload()
    message({
      type: code == 200 ? 'success' : 'error',
      content: msg,
    })
  })
}

const pagination = {
  showPageSize: true,
  pageSize: 20,
}
const request = (params: any, sort: any, filters: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    const query = { ...params, size: params.pageSize }
    Reflect.deleteProperty(query, 'pageSize')
    get(
      {
        url: '/server-system/admin/schedule/page',
        data: query,
      },
      ({ data, code }) => {
        resolve({
          data: data.records,
          total: data.total,
          success: code == 200,
        })
      }
    )
  })
}
const columns = formatColumns([
  { title: 'bean名称', dataIndex: 'beanName', width: 200 },
  { title: 'cron表达式', dataIndex: 'cronExpression', width: 200 },
  { title: '执行参数', dataIndex: 'params', width: 200 },
  {
    title: '任务状态',
    dataIndex: 'status',
    valueEnum: {
      0: { text: '运行', status: 'Success' },
      1: { text: '暂停', status: 'Error' },
    },
    width: 200,
  },
  { title: '任务备注', dataIndex: 'remark', width: 200 },
  { title: '创建时间', dataIndex: 'createTime', width: 200 },
  {
    title: '操作',
    dataIndex: 'operations',
    width: 350,
    fixed: 'right',
    btnList: [
      {
        name: '编辑',
        permission: BtnPermission.编辑,
        handle: ({ record }) => store.handleShow(record),
      },
      {
        name: '删除',
        type: 'popconfirm',
        tip: '确认删除该数据吗?',
        permission: BtnPermission.删除,
        handle: ({ record }) => {
          post({ url: '/server-system/admin/schedule/remove?ids=' + record.jobId }, ({ code, msg }) => {
            if (code == 200) actionRef.value.reload()
            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
      {
        name: '立即执行',
        permission: BtnPermission.编辑,
        handle: ({ record }) => {
          post({ url: 'server-system/admin/schedule/run?ids=' + record.jobId }, ({ code, msg }) => {
            if (code == 200) actionRef.value.reload()
            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
      {
        permission: BtnPermission.编辑,
        name: '运行',
        hidden: ({ record }) => record.status == 0,
        handle: ({ record }) => changeStatus(record),
      },
      {
        permission: BtnPermission.编辑,
        name: '暂停',
        hidden: ({ record }) => record.status == 1,
        handle: ({ record }) => changeStatus(record),
      },
      {
        name: '查看日志',
        permission: BtnPermission.查看,

        handle: ({ record }) => viewStore.handleShow(record),
      },
    ],
  },
])
</script>

<template>
  <div class="page">
    <ProTable :columns="columns" :request="request" row-key="id" :form-ref="ref => (formRef = ref)" :pagination="pagination" header-title="" :scroll="{ x: 1000 }" column-resizable :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <a-button @click="store.handleShow({})" type="primary" v-permission="[BtnPermission.新建]">
          <template #icon>
            <icon-plus />
          </template>
          新建</a-button
        >
      </template>
    </ProTable>
    <ViewRecords v-if="viewStore.show" v-model:show="viewStore.show" :id="viewStore.info.jobId" />
    <TimeModal v-if="store.show" v-model:show="store.show" :id="store.info.jobId" @confirm="actionRef.reload" />
  </div>
</template>
